// 外卖美食数据库 - 基于真实外卖平台菜单
const dishDatabase = {
    米饭类: [
        { name: "黄焖鸡米饭", description: "鸡肉嫩滑，汤汁浓郁，配菜丰富", category: "米饭类", flavor: "鲜香", price: "15-25元", icon: "🍗" },
        { name: "烤肉拌饭", description: "韩式烤肉配米饭，香甜可口", category: "米饭类", flavor: "甜香", price: "18-28元", icon: "🥩" },
        { name: "卤肉饭", description: "台式经典，肥瘦相间，香糯下饭", category: "米饭类", flavor: "咸香", price: "12-20元", icon: "🍖" },
        { name: "日式肥牛饭", description: "嫩滑肥牛配日式调料，清香不腻", category: "米饭类", flavor: "清香", price: "20-30元", icon: "🥩" },
        { name: "双拼饭", description: "两种荤菜搭配，营养丰富", category: "米饭类", flavor: "综合", price: "16-26元", icon: "🍱" },
        { name: "鱼香肉丝盖浇饭", description: "经典川菜，酸甜开胃", category: "米饭类", flavor: "酸甜", price: "14-22元", icon: "🐟" },
        { name: "宫保鸡丁盖浇饭", description: "花生米配鸡丁，香脆可口", category: "米饭类", flavor: "微辣", price: "15-23元", icon: "🥜" },
        { name: "红烧肉盖浇饭", description: "肥而不腻，色泽红亮", category: "米饭类", flavor: "甜香", price: "16-24元", icon: "🍖" },
        { name: "麻婆豆腐盖浇饭", description: "川菜经典，麻辣鲜香", category: "米饭类", flavor: "麻辣", price: "13-21元", icon: "🌶️" },
        { name: "石锅拌饭", description: "韩式经典，蔬菜丰富营养均衡", category: "米饭类", flavor: "清香", price: "18-28元", icon: "🥗" },
        { name: "煲仔饭", description: "广式经典，锅巴香脆", category: "米饭类", flavor: "香脆", price: "20-30元", icon: "🍲" },
        { name: "咖喱鸡肉饭", description: "日式咖喱，温和不辣", category: "米饭类", flavor: "咖喱", price: "16-26元", icon: "🍛" },
        { name: "扬州炒饭", description: "粒粒分明，配菜丰富", category: "米饭类", flavor: "鲜香", price: "15-25元", icon: "🍚" },
        { name: "培根蛋炒饭", description: "西式风味，香浓可口", category: "米饭类", flavor: "香浓", price: "14-24元", icon: "🥓" }
    ],
    面食类: [
        { name: "兰州拉面", description: "清汤牛肉面，汤清肉烂", category: "面食类", flavor: "清香", price: "12-20元", icon: "🍜" },
        { name: "红烧牛肉面", description: "汤汁浓郁，牛肉软烂", category: "面食类", flavor: "浓香", price: "15-25元", icon: "🐄" },
        { name: "番茄牛腩面", description: "酸甜开胃，营养丰富", category: "面食类", flavor: "酸甜", price: "16-26元", icon: "🍅" },
        { name: "重庆小面", description: "麻辣鲜香，重庆特色", category: "面食类", flavor: "麻辣", price: "10-18元", icon: "🌶️" },
        { name: "刀削面", description: "山西特色，面条劲道", category: "面食类", flavor: "鲜香", price: "12-20元", icon: "🔪" },
        { name: "干炒牛河", description: "粤式经典，河粉爽滑", category: "面食类", flavor: "鲜香", price: "16-26元", icon: "🍝" },
        { name: "意大利面", description: "番茄肉酱或奶油口味", category: "面食类", flavor: "西式", price: "18-30元", icon: "🍝" },
        { name: "日式乌冬面", description: "汤汁清淡，面条Q弹", category: "面食类", flavor: "清淡", price: "15-25元", icon: "🍜" },
        { name: "酸辣粉", description: "重庆特色，酸辣开胃", category: "面食类", flavor: "酸辣", price: "8-16元", icon: "🌶️" },
        { name: "螺蛳粉", description: "广西特色，酸辣鲜香", category: "面食类", flavor: "酸辣", price: "12-20元", icon: "🐌" },
        { name: "桂林米粉", description: "广西经典，汤汁鲜美", category: "面食类", flavor: "鲜香", price: "10-18元", icon: "🍜" },
        { name: "老友粉", description: "南宁特色，酸辣开胃", category: "面食类", flavor: "酸辣", price: "12-20元", icon: "🍜" }
    ],
    中餐小炒: [
        { name: "回锅肉", description: "四川家常菜，肥而不腻", category: "中餐小炒", flavor: "香辣", price: "18-28元", icon: "🥩" },
        { name: "糖醋里脊", description: "酸甜可口，老少皆宜", category: "中餐小炒", flavor: "酸甜", price: "16-26元", icon: "🍖" },
        { name: "水煮牛肉", description: "川菜经典，麻辣鲜香", category: "中餐小炒", flavor: "麻辣", price: "22-32元", icon: "🌶️" },
        { name: "酸菜鱼", description: "鱼肉鲜嫩，酸辣开胃", category: "中餐小炒", flavor: "酸辣", price: "25-35元", icon: "🐟" },
        { name: "毛血旺", description: "重庆特色，麻辣鲜香", category: "中餐小炒", flavor: "麻辣", price: "20-30元", icon: "🌶️" },
        { name: "口水鸡", description: "四川凉菜，麻辣鲜香", category: "中餐小炒", flavor: "麻辣", price: "15-25元", icon: "🐔" },
        { name: "辣子鸡", description: "重庆特色，香辣下饭", category: "中餐小炒", flavor: "香辣", price: "18-28元", icon: "🌶️" },
        { name: "蒜蓉粉丝虾", description: "粤菜经典，鲜香嫩滑", category: "中餐小炒", flavor: "鲜香", price: "20-30元", icon: "🦐" },
        { name: "干锅花菜", description: "湘菜特色，香辣下饭", category: "中餐小炒", flavor: "香辣", price: "14-22元", icon: "🥬" },
        { name: "地三鲜", description: "东北家常菜，营养丰富", category: "中餐小炒", flavor: "咸鲜", price: "12-20元", icon: "🍆" },
        { name: "蚂蚁上树", description: "川菜经典，粉丝爽滑", category: "中餐小炒", flavor: "咸鲜", price: "14-22元", icon: "🍜" },
        { name: "手撕包菜", description: "家常小炒，清爽下饭", category: "中餐小炒", flavor: "清香", price: "10-18元", icon: "🥬" }
    ],
    轻食健康: [
        { name: "健身鸡胸藜麦碗", description: "低脂高蛋白，健康营养", category: "轻食健康", flavor: "清淡", price: "25-35元", icon: "🥗" },
        { name: "烟熏三文鱼沙拉", description: "富含Omega-3，营养丰富", category: "轻食健康", flavor: "清香", price: "30-40元", icon: "🐟" },
        { name: "牛油果虾仁沙拉", description: "健康脂肪，口感丰富", category: "轻食健康", flavor: "清香", price: "28-38元", icon: "🥑" },
        { name: "烤南瓜糙米碗", description: "粗粮搭配，营养均衡", category: "轻食健康", flavor: "清甜", price: "22-32元", icon: "🎃" },
        { name: "低温牛肉荞麦面", description: "低脂蛋白质，饱腹感强", category: "轻食健康", flavor: "清香", price: "26-36元", icon: "🥩" }
    ],
    饺子包子: [
        { name: "韭菜鸡蛋饺子", description: "经典搭配，香味浓郁", category: "饺子包子", flavor: "鲜香", price: "12-20元", icon: "🥟" },
        { name: "三鲜水饺", description: "虾仁猪肉韭菜，鲜美可口", category: "饺子包子", flavor: "鲜美", price: "15-25元", icon: "🥟" },
        { name: "白菜猪肉饺子", description: "家常口味，经济实惠", category: "饺子包子", flavor: "咸鲜", price: "10-18元", icon: "🥟" },
        { name: "鲜虾云吞", description: "广式经典，皮薄馅大", category: "饺子包子", flavor: "鲜甜", price: "16-26元", icon: "🥟" },
        { name: "红油抄手", description: "四川特色，麻辣鲜香", category: "饺子包子", flavor: "麻辣", price: "12-20元", icon: "🌶️" },
        { name: "小笼包", description: "江南特色，汤汁丰富", category: "饺子包子", flavor: "鲜美", price: "15-25元", icon: "🥟" },
        { name: "生煎包", description: "上海特色，底部焦脆", category: "饺子包子", flavor: "鲜香", price: "12-20元", icon: "🥟" },
        { name: "锅贴", description: "煎饺经典，外酥内嫩", category: "饺子包子", flavor: "香脆", price: "14-22元", icon: "🥟" }
    ],
    粥类早餐: [
        { name: "皮蛋瘦肉粥", description: "广式经典，营养丰富", category: "粥类早餐", flavor: "鲜香", price: "8-16元", icon: "🍚" },
        { name: "南瓜小米粥", description: "养胃暖身，香甜可口", category: "粥类早餐", flavor: "清甜", price: "6-14元", icon: "🎃" },
        { name: "艇仔粥", description: "广东特色，配菜丰富", category: "粥类早餐", flavor: "鲜美", price: "12-20元", icon: "🍚" },
        { name: "虾蟹粥", description: "海鲜鲜美，营养丰富", category: "粥类早餐", flavor: "鲜甜", price: "18-28元", icon: "🦐" },
        { name: "叉烧肠粉", description: "广式茶点，嫩滑爽口", category: "粥类早餐", flavor: "鲜香", price: "10-18元", icon: "🥢" },
        { name: "鲜虾肠粉", description: "虾仁饱满，皮薄馅大", category: "粥类早餐", flavor: "鲜甜", price: "12-20元", icon: "🦐" },
        { name: "茶叶蛋", description: "经典小食，香味浓郁", category: "粥类早餐", flavor: "咸香", price: "3-6元", icon: "🥚" }
    ],
    汤煲类: [
        { name: "羊杂汤", description: "西北特色，温补暖身", category: "汤煲类", flavor: "鲜香", price: "15-25元", icon: "🐑" },
        { name: "牛尾汤", description: "营养丰富，汤汁浓郁", category: "汤煲类", flavor: "浓香", price: "25-35元", icon: "🐄" },
        { name: "萝卜排骨汤", description: "家常汤品，清香不腻", category: "汤煲类", flavor: "清香", price: "12-20元", icon: "🥕" },
        { name: "酸萝卜老鸭汤", description: "四川特色，酸辣开胃", category: "汤煲类", flavor: "酸香", price: "18-28元", icon: "🦆" },
        { name: "鸡公煲", description: "广东特色，香辣下饭", category: "汤煲类", flavor: "香辣", price: "25-35元", icon: "🐔" },
        { name: "羊蝎子", description: "北京特色，温补美味", category: "汤煲类", flavor: "鲜香", price: "30-40元", icon: "🐑" }
    ],
    麻辣烫: [
        { name: "骨汤麻辣烫", description: "汤底浓郁，配菜丰富", category: "麻辣烫", flavor: "麻辣", price: "15-25元", icon: "🌶️" },
        { name: "番茄麻辣烫", description: "酸甜开胃，不那么辣", category: "麻辣烫", flavor: "酸甜", price: "12-22元", icon: "🍅" },
        { name: "干拌麻辣烫", description: "无汤版本，香辣过瘾", category: "麻辣烫", flavor: "香辣", price: "14-24元", icon: "🌶️" },
        { name: "酸辣金汤冒菜", description: "四川特色，酸辣开胃", category: "麻辣烫", flavor: "酸辣", price: "16-26元", icon: "🌶️" },
        { name: "麻辣香锅", description: "干锅版麻辣烫，香辣下饭", category: "麻辣烫", flavor: "麻辣", price: "18-28元", icon: "🌶️" }
    ],
    烧烤炸物: [
        { name: "烤五花肉", description: "肥瘦相间，香嫩多汁", category: "烧烤炸物", flavor: "香烤", price: "15-25元", icon: "🥩" },
        { name: "烤羊肉串", description: "新疆风味，孜然香浓", category: "烧烤炸物", flavor: "孜然", price: "2-5元/串", icon: "🍖" },
        { name: "烤鸡翅", description: "外焦内嫩，香味扑鼻", category: "烧烤炸物", flavor: "香烤", price: "3-6元/个", icon: "🍗" },
        { name: "烤生蚝", description: "蒜蓉粉丝，鲜美可口", category: "烧烤炸物", flavor: "鲜香", price: "8-15元/个", icon: "🦪" },
        { name: "韩式炸鸡", description: "酥脆外皮，多种口味", category: "烧烤炸物", flavor: "香脆", price: "20-35元", icon: "🍗" },
        { name: "炸鸡排", description: "台式经典，外酥内嫩", category: "烧烤炸物", flavor: "香脆", price: "12-20元", icon: "🍗" },
        { name: "炸串拼盘", description: "多种炸物，丰富选择", category: "烧烤炸物", flavor: "香脆", price: "15-30元", icon: "🍢" }
    ],
    西式快餐: [
        { name: "牛肉汉堡", description: "经典美式，牛肉饼香嫩", category: "西式快餐", flavor: "香嫩", price: "15-25元", icon: "🍔" },
        { name: "芝士汉堡", description: "芝士浓郁，口感丰富", category: "西式快餐", flavor: "浓郁", price: "12-22元", icon: "🍔" },
        { name: "鸡腿堡", description: "炸鸡腿配蔬菜，营养均衡", category: "西式快餐", flavor: "香脆", price: "14-24元", icon: "🍔" },
        { name: "薯条", description: "金黄酥脆，经典配菜", category: "西式快餐", flavor: "香脆", price: "8-15元", icon: "🍟" },
        { name: "玛格丽特披萨", description: "意式经典，番茄芝士", category: "西式快餐", flavor: "清香", price: "25-40元", icon: "🍕" },
        { name: "意式辣肠披萨", description: "辣肠配芝士，香辣可口", category: "西式快餐", flavor: "香辣", price: "30-45元", icon: "🍕" },
        { name: "夏威夷披萨", description: "菠萝火腿，酸甜口味", category: "西式快餐", flavor: "酸甜", price: "28-42元", icon: "🍕" }
    ],
    日韩料理: [
        { name: "三文鱼寿司", description: "新鲜三文鱼，入口即化", category: "日韩料理", flavor: "鲜美", price: "25-40元", icon: "🍣" },
        { name: "加州卷", description: "牛油果蟹肉，口感丰富", category: "日韩料理", flavor: "清香", price: "20-35元", icon: "🍣" },
        { name: "天妇罗", description: "日式炸物，外酥内嫩", category: "日韩料理", flavor: "香脆", price: "18-30元", icon: "🍤" },
        { name: "韩式烤肉饭", description: "烤肉配泡菜，韩式风味", category: "日韩料理", flavor: "香烤", price: "22-35元", icon: "🥩" },
        { name: "紫菜包饭", description: "韩式经典，营养丰富", category: "日韩料理", flavor: "清香", price: "12-20元", icon: "🍙" },
        { name: "泰式冬阴功", description: "酸辣开胃，泰式经典", category: "日韩料理", flavor: "酸辣", price: "15-25元", icon: "🍲" },
        { name: "越式河粉", description: "清汤河粉，越南风味", category: "日韩料理", flavor: "清香", price: "16-26元", icon: "🍜" }
    ],
    小吃甜品: [
        { name: "凉皮", description: "陕西特色，酸辣爽口", category: "小吃甜品", flavor: "酸辣", price: "6-12元", icon: "🥒" },
        { name: "肉夹馍", description: "陕西经典，肉香馍脆", category: "小吃甜品", flavor: "香脆", price: "8-15元", icon: "🥙" },
        { name: "煎饼果子", description: "天津特色，香脆可口", category: "小吃甜品", flavor: "香脆", price: "6-12元", icon: "🥞" },
        { name: "手抓饼", description: "台湾小食，层次丰富", category: "小吃甜品", flavor: "香脆", price: "5-10元", icon: "🥞" },
        { name: "豆腐脑", description: "嫩滑豆腐，咸甜可选", category: "小吃甜品", flavor: "嫩滑", price: "4-8元", icon: "🥛" },
        { name: "双皮奶", description: "广式甜品，奶香浓郁", category: "小吃甜品", flavor: "奶香", price: "8-15元", icon: "🥛" },
        { name: "杨枝甘露", description: "港式甜品，芒果椰浆", category: "小吃甜品", flavor: "清甜", price: "12-20元", icon: "🥭" },
        { name: "蛋挞", description: "港式茶点，酥脆香甜", category: "小吃甜品", flavor: "香甜", price: "3-6元/个", icon: "🥧" },
        { name: "珍珠奶茶", description: "台式经典，Q弹珍珠", category: "小吃甜品", flavor: "奶香", price: "10-18元", icon: "🧋" },
        { name: "椰椰芒芒", description: "椰浆芒果，热带风味", category: "小吃甜品", flavor: "清甜", price: "15-25元", icon: "🥥" }
    ],
    汤粉类: [
        { name: "南昌拌粉", description: "江西特色，配瓦罐汤", category: "汤粉类", flavor: "鲜香", price: "12-20元", icon: "🍜" },
        { name: "湖南米粉", description: "湖南风味，酸辣开胃", category: "汤粉类", flavor: "酸辣", price: "10-18元", icon: "🍜" },
        { name: "广东竹升面", description: "面条劲道，汤汁清香", category: "汤粉类", flavor: "清香", price: "14-22元", icon: "🍜" },
        { name: "花甲粉", description: "海鲜鲜美，汤汁浓郁", category: "汤粉类", flavor: "鲜美", price: "16-26元", icon: "🐚" },
        { name: "土豆粉", description: "东北特色，粉条爽滑", category: "汤粉类", flavor: "鲜香", price: "8-16元", icon: "🥔" }
    ],
    DIY套餐: [
        { name: "一荤两素套餐", description: "营养均衡，经济实惠", category: "DIY套餐", flavor: "综合", price: "15-25元", icon: "🍱" },
        { name: "双拼卤味", description: "两种卤味配菜", category: "DIY套餐", flavor: "卤香", price: "12-22元", icon: "🍖" },
        { name: "健身餐", description: "低脂高蛋白，健康搭配", category: "DIY套餐", flavor: "清淡", price: "20-30元", icon: "💪" },
        { name: "素食套餐", description: "全素搭配，清淡健康", category: "DIY套餐", flavor: "清淡", price: "12-20元", icon: "🥬" }
    ]
};

// 全局变量
let currentDish = null;
let isAnimating = false;

// DOM 元素
const recommendBtn = document.getElementById('recommendBtn');
const dishDisplay = document.getElementById('dishDisplay');
const loadingOverlay = document.getElementById('loadingOverlay');
const categoryBtns = document.querySelectorAll('.category-btn');
const dailyRecommendations = document.getElementById('dailyRecommendations');

// 初始化
document.addEventListener('DOMContentLoaded', function() {
    initializePage();
    bindEvents();
    generateDailyRecommendations();
});

// 初始化页面
function initializePage() {
    // 添加页面加载动画
    document.body.classList.add('fade-in');
    
    // 设置随机背景渐变
    setRandomBackground();
    
    // 添加触摸支持
    addTouchSupport();
    
    // 初始化语音合成
    initializeSpeechSynthesis();
}

// 绑定事件
function bindEvents() {
    // 推荐按钮点击事件
    recommendBtn.addEventListener('click', () => {
        const activeCategory = document.querySelector('.category-btn.active');
        if (activeCategory && activeCategory.dataset.cuisine !== 'all') {
            getRandomDishByCuisine(activeCategory.dataset.cuisine);
        } else {
            getRandomDish();
        }
    });
    
    // 菜系分类按钮点击事件
    categoryBtns.forEach(btn => {
        btn.addEventListener('click', function() {
            const category = this.dataset.cuisine;
            getRandomDishByCuisine(category);
            updateActiveCategory(this);
        });
    });
    
    // 键盘事件
    document.addEventListener('keydown', function(e) {
        if (e.code === 'Space' && !isAnimating) {
            e.preventDefault();
            getRandomDish();
        }
    });
}

// 获取随机菜品
function getRandomDish() {
    if (isAnimating) return;
    
    showLoading();
    
    setTimeout(() => {
        const allDishes = getAllDishes();
        const randomDish = allDishes[Math.floor(Math.random() * allDishes.length)];
        displayDish(randomDish);
        hideLoading();
        clearActiveCategory();
    }, 1500);
}

// 根据菜系获取随机菜品
function getRandomDishByCuisine(category) {
    if (isAnimating) return;
    
    showLoading();
    
    setTimeout(() => {
        const dishes = dishDatabase[category] || [];
        if (dishes.length > 0) {
            const randomDish = dishes[Math.floor(Math.random() * dishes.length)];
            displayDish(randomDish);
            
            // 语音播报
            speakDishName(randomDish.name);
        }
        hideLoading();
    }, 1200);
}

// 获取所有菜品
function getAllDishes() {
    const allDishes = [];
    for (const cuisine in dishDatabase) {
        allDishes.push(...dishDatabase[cuisine]);
    }
    return allDishes;
}

// 显示菜品
function displayDish(dish) {
    currentDish = dish;
    
    const dishCard = dishDisplay.querySelector('.dish-card');
    const dishImage = dishCard.querySelector('.dish-image');
    const dishName = dishCard.querySelector('.dish-name');
    const dishTags = dishCard.querySelector('.dish-tags');
    const dishDescription = dishCard.querySelector('.dish-description');
    const dishDetails = dishCard.querySelector('.dish-details');
    
    // 添加高亮动画
    dishCard.classList.add('highlight');
    setTimeout(() => dishCard.classList.remove('highlight'), 1000);
    
    // 更新菜品图片（使用emoji）
    dishImage.innerHTML = `<div class="placeholder-image">${dish.icon}</div>`;
    
    // 更新菜品信息
    dishName.textContent = `${dish.icon} ${dish.name}`;
    dishDescription.textContent = dish.description;
    
    // 更新标签
    dishTags.innerHTML = `
        <span class="tag">${dish.category}</span>
        <span class="tag">${dish.flavor}</span>
        <span class="tag">${dish.price}</span>
    `;
    
    // 更新详细信息
    dishDetails.innerHTML = `
        <div class="detail-item">
            <span class="detail-label">分类：</span>
            <span class="detail-value">${dish.category}</span>
        </div>
        <div class="detail-item">
            <span class="detail-label">口味：</span>
            <span class="detail-value">${dish.flavor}</span>
        </div>
        <div class="detail-item">
            <span class="detail-label">价格：</span>
            <span class="detail-value">${dish.price}</span>
        </div>
        <div class="order-actions">
            <button class="order-btn" onclick="orderDish('${dish.name}')">
                📱 立即点餐
            </button>
            <button class="search-btn" onclick="searchDish('${dish.name}')">
                🔍 搜索商家
            </button>
        </div>
    `;
    
    // 添加滑入动画
    dishDisplay.classList.add('slide-in');
    setTimeout(() => dishDisplay.classList.remove('slide-in'), 800);
    
    // 添加到收藏按钮
    addToFavoriteButton(dish);
}

// 显示加载动画
function showLoading() {
    isAnimating = true;
    loadingOverlay.classList.add('show');
    recommendBtn.disabled = true;
}

// 隐藏加载动画
function hideLoading() {
    isAnimating = false;
    loadingOverlay.classList.remove('show');
    recommendBtn.disabled = false;
}

// 更新活跃分类
function updateActiveCategory(activeBtn) {
    categoryBtns.forEach(btn => btn.classList.remove('active'));
    activeBtn.classList.add('active');
}

// 清除活跃分类
function clearActiveCategory() {
    categoryBtns.forEach(btn => btn.classList.remove('active'));
}

// 生成每日推荐
function generateDailyRecommendations() {
    const allDishes = getAllDishes();
    const shuffled = allDishes.sort(() => 0.5 - Math.random());
    const recommendations = shuffled.slice(0, 6);
    
    dailyRecommendations.innerHTML = recommendations.map(dish => `
        <div class="recommendation-card" onclick="displayDish(${JSON.stringify(dish).replace(/"/g, '&quot;')})">
            <div class="daily-icon">${dish.icon}</div>
            <div class="daily-content">
                <h4>${dish.name}</h4>
                <p>${dish.description.substring(0, 50)}...</p>
                <div class="dish-tags" style="margin-top: 10px;">
                    <span class="tag" style="font-size: 0.8rem;">${dish.category}</span>
                    <span class="price-tag" style="font-size: 0.8rem;">${dish.price}</span>
                </div>
            </div>
        </div>
    `).join('');
}

// 设置随机背景
function setRandomBackground() {
    const gradients = [
        'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
        'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
        'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
        'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
        'linear-gradient(135deg, #fa709a 0%, #fee140 100%)',
        'linear-gradient(135deg, #a8edea 0%, #fed6e3 100%)',
        'linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%)',
        'linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%)'
    ];
    
    const randomGradient = gradients[Math.floor(Math.random() * gradients.length)];
    document.body.style.background = randomGradient;
}

// 工具函数：获取今日日期字符串
function getTodayString() {
    const today = new Date();
    return `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
}

// 工具函数：根据日期生成固定的推荐
function getDailyRecommendationsByDate() {
    const dateString = getTodayString();
    const seed = dateString.split('').reduce((a, b) => {
        a = ((a << 5) - a) + b.charCodeAt(0);
        return a & a;
    }, 0);
    
    Math.seedrandom = function(seed) {
        const m = 0x80000000;
        const a = 1103515245;
        const c = 12345;
        let state = seed ? seed : Math.floor(Math.random() * (m - 1));
        
        return function() {
            state = (a * state + c) % m;
            return state / (m - 1);
        };
    };
    
    const seededRandom = Math.seedrandom(Math.abs(seed));
    const allDishes = getAllDishes();
    const shuffled = allDishes.sort(() => seededRandom() - 0.5);
    
    return shuffled.slice(0, 6);
}

// 添加触摸支持（移动端）
let touchStartY = 0;
let touchEndY = 0;

document.addEventListener('touchstart', function(e) {
    touchStartY = e.changedTouches[0].screenY;
});

document.addEventListener('touchend', function(e) {
    touchEndY = e.changedTouches[0].screenY;
    handleSwipe();
});

function handleSwipe() {
    const swipeThreshold = 50;
    const diff = touchStartY - touchEndY;
    
    if (Math.abs(diff) > swipeThreshold && !isAnimating) {
        if (diff > 0) {
            // 向上滑动，获取随机菜品
            getRandomDish();
        }
    }
}

// 语音合成功能
let speechSynthesisInstance = null;

function initializeSpeechSynthesis() {
    if ('speechSynthesis' in window) {
        speechSynthesisInstance = window.speechSynthesis;
    }
}

function speakDishName(dishName) {
    if (speechSynthesisInstance) {
        const utterance = new SpeechSynthesisUtterance(`今天推荐您吃${dishName}`);
        utterance.lang = 'zh-CN';
        utterance.rate = 0.8;
        speechSynthesisInstance.speak(utterance);
    }
}

// 点餐功能
function orderDish(dishName) {
    showNotification(`正在为您搜索"${dishName}"的商家...`);
    // 这里可以集成外卖平台API或跳转到外卖应用
    setTimeout(() => {
        showNotification('请在外卖APP中搜索该菜品名称');
    }, 1500);
}

// 搜索商家功能
function searchDish(dishName) {
    // 模拟搜索功能
    const searchTips = [
        '建议在美团、饿了么搜索',
        '可以尝试搜索关键词',
        '附近商家推荐中...',
        '正在匹配最佳商家'
    ];
    
    const randomTip = searchTips[Math.floor(Math.random() * searchTips.length)];
    showNotification(`${randomTip}: "${dishName}"`);
}

// 添加触摸支持
function addTouchSupport() {
    if (recommendBtn) {
        recommendBtn.addEventListener('touchstart', function(e) {
            this.style.transform = 'scale(0.95)';
        });
        
        recommendBtn.addEventListener('touchend', function(e) {
            this.style.transform = 'scale(1)';
        });
    }
}

// 收藏功能
let favoriteDishes = JSON.parse(localStorage.getItem('favoriteDishes')) || [];

function addToFavoriteButton(dish) {
    const dishDetails = document.getElementById('dishDetails') || dishDisplay.querySelector('.dish-details');
    if (!dishDetails) return;
    
    const existingBtn = dishDetails.querySelector('.favorite-btn');
    
    if (existingBtn) {
        existingBtn.remove();
    }
    
    const favoriteBtn = document.createElement('button');
    favoriteBtn.className = 'favorite-btn';
    favoriteBtn.innerHTML = favoriteDishes.some(fav => fav.name === dish.name) ? '❤️ 已收藏' : '🤍 收藏';
    
    favoriteBtn.addEventListener('click', () => {
        toggleFavorite(dish, favoriteBtn);
    });
    
    dishDetails.appendChild(favoriteBtn);
}

function toggleFavorite(dish, button) {
    const index = favoriteDishes.findIndex(fav => fav.name === dish.name);
    
    if (index > -1) {
        favoriteDishes.splice(index, 1);
        button.innerHTML = '🤍 收藏';
        showNotification('已取消收藏');
    } else {
        favoriteDishes.push(dish);
        button.innerHTML = '❤️ 已收藏';
        showNotification('已添加到收藏');
    }
    
    localStorage.setItem('favoriteDishes', JSON.stringify(favoriteDishes));
}

// 显示通知
function showNotification(message) {
    const notification = document.createElement('div');
    notification.className = 'notification';
    notification.textContent = message;
    notification.style.cssText = `
        position: fixed;
        top: 20px;
        right: 20px;
        background: #4CAF50;
        color: white;
        padding: 15px 20px;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        z-index: 1001;
        animation: slideInRight 0.3s ease-out;
        font-size: 14px;
        max-width: 300px;
    `;
    
    document.body.appendChild(notification);
    
    setTimeout(() => {
        notification.style.animation = 'slideOutRight 0.3s ease-out';
        setTimeout(() => {
            if (notification.parentNode) {
                notification.parentNode.removeChild(notification);
            }
        }, 300);
    }, 2000);
}

// 添加CSS动画
const style = document.createElement('style');
style.textContent = `
    @keyframes slideInRight {
        from {
            transform: translateX(100%);
            opacity: 0;
        }
        to {
            transform: translateX(0);
            opacity: 1;
        }
    }
    
    @keyframes slideOutRight {
        from {
            transform: translateX(0);
            opacity: 1;
        }
        to {
            transform: translateX(100%);
            opacity: 0;
        }
    }
`;
document.head.appendChild(style);